function writeData() {
    Office.context.document.setSelectedDataAsync([["red"],["green"],["blue"]], function (asyncResult) {
        if (asyncResult.status === "failed") {
            writeToPage('Error: ' + asyncResult.error.message);
        }
    });
}
function writeToPage(text) {
    document.getElementById('results').innerText = text;
}
function readData() {
    Office.context.document.getSelectedDataAsync("matrix", function (asyncResult) {
        if (asyncResult.status === "failed") {
            writeToPage('Error: ' + asyncResult.error.message);
        } 
        else{
            writeToPage('Selected data: ' + asyncResult.value);
        }
    });
}
function bindData() {
    Office.context.document.bindings.addFromSelectionAsync("matrix", { id: 'myBinding' }, 
        function (asyncResult) {
            if (asyncResult.status === "failed") {
                writeToPage('Error: ' + asyncResult.error.message);
            } else {
                writeToPage('Added binding with type: ' + asyncResult.value.type + ' and id: ' +
                    asyncResult.value.id);
            }
        });
}
function readBoundData() {
    Office.select("bindings#myBinding").getDataAsync({ coercionType: "matrix" }, 
        function (asyncResult) {
            if (asyncResult.status === "failed") {
                writeToPage('Error: ' + asyncResult.error.message);
            } else {
                writeToPage('Selected data: ' + asyncResult.value);
            }
        });
}
function addEvent() {
    Office.select("bindings#myBinding").addHandlerAsync("bindingDataChanged", myHandler, function (asyncResult) {
        if (asyncResult.status === "failed") {
            writeToPage('Error: ' + asyncResult.error.message);
        } else {
            writeToPage('Added event handler');
        }
    });
}
function myHandler(eventArgs) {
    eventArgs.binding.getDataAsync({ coerciontype: "matrix" }, function (asyncResult) {

        if (asyncResult.status === "failed") {
            writeToPage('Error: ' + asyncResult.error.message);
        } else {
            writeToPage('Bound data: ' + asyncResult.value);
        }
    });
}